package GUI;

import handlers.ColourSchema;

import java.awt.Color;
import java.awt.event.*;

public class Login extends javax.swing.JFrame {
	
	private static final long serialVersionUID = 1L;
	//TODO Props
	private String username;
	private char[] password;
	//Colours
	ColourSchema colours;
	Color primary;
	Color secondary;
	
    /**
     * Creates new form Login
     */
    public Login() {
    	//init colours
    	colours = new ColourSchema();
    	primary = colours.getPrimary();
    	secondary = colours.getSecondary();
   
    	initComponents();
    }
    
    private void initComponents() {
        login = new javax.swing.JPanel();
        user = new javax.swing.JTextField();
        pass = new javax.swing.JPasswordField();
        loginBtn = new javax.swing.JButton();
        
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Tiliches - Login");
        getContentPane().setBackground(primary);

        login.setBackground(secondary);

        //TODO UserField
        user.setFont(new java.awt.Font("Open Sans", 0, 14)); // NOI18N
        user.setText("Username");
        user.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	userFieldActionPerformed(evt);
            }
        });
        user.addMouseListener(new MouseAdapter(){
            @Override
            public void mouseClicked(MouseEvent e){
                if (user.getText().equals("Username")) {
                	user.setText("");
                }
            }
        });
        
        
        //TODO PassField
        pass.setFont(new java.awt.Font("Open Sans", 0, 14)); // NOI18N
        pass.setText("Password");
        pass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	passFieldActionPerformed(evt);
            }
        });
        pass.addMouseListener(new MouseAdapter(){
            @Override
            public void mouseClicked(MouseEvent e){
                pass.setText("");
            }
        });
        
        //TODO LoginBtn
        loginBtn.setBackground(primary);
        loginBtn.setFont(new java.awt.Font("Open Sans", 0, 18)); // NOI18N
        loginBtn.setForeground(Color.WHITE);
        loginBtn.setText("Login");
        loginBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout loginLayout = new javax.swing.GroupLayout(login);
        login.setLayout(loginLayout);
        loginLayout.setHorizontalGroup(
            loginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginLayout.createSequentialGroup()
                .addGroup(loginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(loginLayout.createSequentialGroup()
                        .addGap(121, 121, 121)
                        .addGroup(loginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(user, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(pass, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(loginLayout.createSequentialGroup()
                        .addGap(160, 160, 160)
                        .addComponent(loginBtn)))
                .addContainerGap(123, Short.MAX_VALUE))
        );
        loginLayout.setVerticalGroup(
            loginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginLayout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(user, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pass, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(loginBtn)
                .addContainerGap(42, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(login, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(login, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }
    
    //TODO ActionListeners
    private void loginBtnActionPerformed(java.awt.event.ActionEvent evt) {
    	username = user.getText();
    	password = pass.getPassword();
    	
    	handlers.WindowManager switcher = new handlers.WindowManager();
    	switcher.login(username, password);
    }
    private void userFieldActionPerformed(java.awt.event.ActionEvent evt) {
    	user.setText("");
    }
    private void passFieldActionPerformed(java.awt.event.ActionEvent evt) {
    	//pass.setText("");
    	username = user.getText();
    	password = pass.getPassword();
    	
    	handlers.WindowManager switcher = new handlers.WindowManager();
    	switcher.login(username, password);
    }

    // Variables declaration - do not modify
    private javax.swing.JButton loginBtn;
    private javax.swing.JPanel login;
    private javax.swing.JPasswordField pass;
    private javax.swing.JTextField user;
}